using Microsoft.VisualStudio.Modeling.Diagrams;
using EnvDTE;
using DslFactory.Utilities.DragDrop;
using DslFactory.Utilities.DteCodeModel;
using Microsoft.VisualStudio.Modeling;
using EnvDTE80;
using System;
using System.Net.Security;
using System.Collections.Generic;

namespace Dslfactory.wcfLanguage
{
 public partial class wcfLanguageDiagram
 {
  /// <summary>
  /// The root model element represented by the diagram
  /// </summary>
  ServicesDefinition ServicesDefinition
  {
   get
   {
    return ModelElement as ServicesDefinition;
   }
  }

  private DataClassShape CreateShapeForDataClass(DataClass newElement)
  {
   if (newElement is DataContract)
    return new DataContractShape(Partition);
   else if (newElement is MessageContract)
    return new MessageContractShape(Partition);
   else
    return null;
  }
  /// <summary>
  /// First draw. We update the model from the code content, and subscribe to KnownCodeTypes
  /// events
  /// </summary>
  protected override void OnBeforePaint()
  {
   base.OnBeforePaint();
   if (reverseEngineering == null)
   {
    Store.RuleManager.EnableRule(typeof(RepresentationChangedRefreshesComparmentsOrRole));
    Store.RuleManager.EnableRule(typeof(UsageAddedAddsMemberRule));
    Store.RuleManager.EnableRule(typeof(Dslfactory.wcfLanguage.wcfLanguageDomainModel.MethodPropertiesCoherence));
    IncrementalCodeGeneration.SubsribeToStoreEvents(Store);

    reverseEngineering = new ReverseEngineering();
    ReverseEngineering.UpdateModel(ServicesDefinition);
    reverseEngineering.SubscribeToKnownTypesEvents(ServicesDefinition);
   }
  }

  ReverseEngineering reverseEngineering = null;


  /// <summary>
  /// If we drop a type if it can be transformed into a model element, we accept it.
  /// </summary>
  /// <param name="e"></param>
  public override void OnDragOver(DiagramDragEventArgs e)
  {
   foreach (CodeType codeType in DragDropFromClassView.GetDroppedCodeTypes(Store, e.Data))
   {
    if (ReverseEngineering.CanBeMerged(codeType))
    {
     e.Effect = System.Windows.Forms.DragDropEffects.Copy;
     return;
    }
   }
   base.OnDragOver(e);
  }

  /// <summary>
  /// We consume the codetypes
  /// </summary>
  /// <param name="e"></param>
  public override void OnDragDrop(DiagramDragEventArgs e)
  {
   foreach (CodeType codeType in DragDropFromClassView.GetDroppedCodeTypes(Store, e.Data))
    ReverseEngineering.Merge(ServicesDefinition, codeType);
   base.OnDragDrop(e);
  }

 }
}
